package com.nfu.zlsp.dev.controller;

import com.nfu.zlsp.dev.common.constants.TheConstant;
import com.nfu.zlsp.dev.common.utils.PageUtils;
import com.nfu.zlsp.dev.common.utils.R;
import com.nfu.zlsp.dev.entity.ConsignEntity;
import com.nfu.zlsp.dev.entity.UserEntity;
import com.nfu.zlsp.dev.service.ConsignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;



/**
 * 
 *
 * @author txh
 * @email hunterapeman@gmail.com
 * @date 2023-03-12 01:54:16
 */
@RestController
@RequestMapping("dev/consign")
public class ConsignController {
    @Autowired
    private ConsignService consignService;



    /**
     * 查询承运人所发起的订单（托运放的”智能查询获取数据“）
     */
    @RequestMapping("/queryConsignPage")
    public R queryConsignPage(@RequestParam Map<String, Object> params){

        PageUtils page = consignService.queryConsignPageByInfo(params);
        page.setTotalCount(page.getList().size());
        int totalPage = page.getTotalCount()% page.getPageSize()==0?page.getTotalCount()/ page.getPageSize():page.getTotalCount()/ page.getPageSize()+1;
        page.setTotalPage(totalPage);
        return R.ok().put("page", page);
    }




    /**
     * 通过id删除订单
     * @param id
     * @return
     */
    @RequestMapping("/cancelConsign/{id}")
    //@RequiresPermissions("dev:consign:update")
    public R cancelConsign(@PathVariable("id") String id){
        consignService.cancelConsignById(id);
        return R.ok();
    }


    /**
     * 查询我的发布
     * @param params
     * @param session
     * @return
     */
    @RequestMapping("/getMyConsignPage")
    public R queryConsignPage(@RequestParam Map<String, Object> params,HttpSession session){
        UserEntity user = (UserEntity) session.getAttribute(TheConstant.USER_INFO);
        if(user == null){
            return R.ok().put("data","登录信息过期，请重新登录用户");
        }
        PageUtils page = consignService.queryMyConsignPage(user);
        return R.ok().put("page", page);
    }




    @RequestMapping("/test")
    public R test(HttpSession session){

        UserEntity attribute = (UserEntity) session.getAttribute(TheConstant.USER_INFO);

        System.out.println(attribute);
        return R.ok();
    }



    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("dev:consign:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = consignService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("dev:consign:info")
    public R info(@PathVariable("id") String id){
		ConsignEntity consign = consignService.getById(id);

        return R.ok().put("consign", consign);
    }

    /**
     * 保存
     */
    @Transactional
    @RequestMapping("/save")
    public R save(@RequestBody ConsignEntity consign, HttpSession session){
        UserEntity user = (UserEntity) session.getAttribute(TheConstant.USER_INFO);
        if(user == null){
            return R.ok().put("data","订单发布失败,请重新登录用户");
        }
        consign.setCUserid(user.getId());
        consign.setCreationTime(new Date());
        consignService.save(consign);
        return R.ok();
    }

    /**
     * 修改订单
     */
    @RequestMapping("/update")
    //@RequiresPermissions("dev:consign:update")
    public R update(@RequestBody ConsignEntity consign){
		consignService.updateById(consign);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("dev:consign:delete")
    public R delete(@RequestBody String[] ids){
		consignService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
